package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FrmCadAtv extends Activity {
	private Button btnCadAtv;
	private Button btnClose;
	private Spinner cbCodDisciplina;
	private EditText edtCodAtv;
	private EditText edtNomAtv;
	private CheckBox cbAtvDisp;

	private void limpaEdt(){
		edtCodAtv.setText("");
		edtNomAtv.setText("");
		edtCodAtv.requestFocus();
	}
	
	private void carregaBtn() {
		btnCadAtv = (Button) findViewById(R.FrmCadAtv.btnCad);
		btnClose = (Button) findViewById(R.FrmCadAtv.btnClose);
		cbCodDisciplina = (Spinner) findViewById(R.FrmCadAtv.cbDisciplina);
		edtCodAtv = (EditText) findViewById(R.FrmCadAtv.edtCodAtv);
		edtNomAtv = (EditText) findViewById(R.FrmCadAtv.edtNomAtv);
		cbAtvDisp = (CheckBox) findViewById(R.FrmCadAtv.cbAtvDisp);
		
		String[] codDisciplina;
		try {
			WebServiceClient ws = new WebServiceClient();
			codDisciplina = ws.getDisciplinaProfessor("getDisciplinaProfessor",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm());
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codDisciplina);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbCodDisciplina.setAdapter(adp);
		} catch (Exception e) {
			Toast t = Toast.makeText(this, "Erro na lista - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}

		btnCadAtv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int atvDisp = 0;
				if (cbAtvDisp.isChecked() == true)
					atvDisp = 1;
				try {
					WebServiceClient ws = new WebServiceClient();
					String s = ws.addAtividade("addAtividade", Definicoes
							.getRaAdm(), Definicoes.getSenhaAdm(), edtCodAtv
							.getText().toString(), edtNomAtv.getText()
							.toString(), Definicoes.getSessao(cbCodDisciplina
							.getSelectedItem().toString(), "|", 1), atvDisp);
					if (s.equals("true")){
						Toast t = Toast.makeText(FrmCadAtv.this, 
								"Atividade Cadastrada com Secuesso", 
								Toast.LENGTH_LONG);
						t.show();	
						limpaEdt();
					}else{
						Toast t = Toast.makeText(FrmCadAtv.this, 
								"Erro ao Cadastrar Atividade", 
								Toast.LENGTH_LONG);
						t.show();	
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmCadAtv.this, 
							Definicoes._erroServer+" "+e.getMessage(), 
							Toast.LENGTH_LONG);
					t.show();	
				}
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_cad_atv);
		carregaBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_cad_atv, menu);
		return true;
	}

}
